package com.inji.spark.biz.service;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.inji.spark.biz.pojo.CreditLoanDetail;
import com.inji.spark.biz.pojo.CreditReportData;
import com.inji.spark.biz.util.JdbcUtils;

/**
 * 八方征信因子
 * @author liukz
 *
 */
@Service
public class FcBfangService {
	private static Logger logger = LoggerFactory.getLogger(FcBfangService.class);

	
	
	/**
	 * 房产按揭/抵押数量
	 * @param resp
	 * @param userId
	 * @throws SQLException 
	 */
	
	public boolean houseLoanCount(CreditReportData resp,String userId) throws Exception{
		boolean rtFlag = false;
		int count = 0; 
		List<CreditLoanDetail> listLoanDetail = resp.getListLoanDetail();
		if(CollectionUtils.isEmpty(listLoanDetail)){
			return rtFlag;
		}
		List<String> list = Arrays.asList("个人住房贷款","个人商用房（包括商住两用）贷款","个人住房公积金贷款"); 
		for (CreditLoanDetail creditLoanDetail : listLoanDetail) {
			if(list.contains(creditLoanDetail.getCreditCardType())){
				count++;
			}
		}
/*		UcPersonInfo record = new UcPersonInfo();
		record.setHouseLoanCount(count);
		record.setUpdateDate(new Date());
		
		UcPersonInfoExample personInfoExample = new UcPersonInfoExample();
		personInfoExample.createCriteria().andUserIdEqualTo(userId);
		
		int updateCount= ucPersonInfoMapper.updateByExampleSelective(record, personInfoExample);
		if(updateCount>0){
			logger.info("房产按揭/抵押数量因子更新成功！");
			rtFlag = true;
		}*/
		//处理逻辑后更新表uc_basic_info
		Connection connection =JdbcUtils.getConnectionJdbc();
		PreparedStatement ps = connection.prepareStatement("update uc_person_info set house_loan_count=? where user_id=? ");
		ps.setInt(1,count);
		ps.setString(2,userId);
		int updateCount = ps.executeUpdate();
		if(updateCount>0){
			logger.info("房产按揭/抵押数量因子更新成功！");
			rtFlag = true;
		}
		return rtFlag;
	}
	/**
	 * 无抵押贷款总额
	 * @param resp
	 * @param userId
	 * @throws Exception 
	 */
	public boolean noPledgeLoanAmount(CreditReportData resp,String userId) throws Exception{
		boolean rtFlag = false;
		List<CreditLoanDetail> listLoanDetail = resp.getListLoanDetail();
		if(CollectionUtils.isEmpty(listLoanDetail)){
			return rtFlag;
		}
		//其他贷款：未结清的贷款总额 - 未结清的个人汽车贷款总额
		BigDecimal noSettleTotal = BigDecimal.ZERO;
		BigDecimal noSettleCarTotal = BigDecimal.ZERO;
		BigDecimal noPledgeLoanAmount = BigDecimal.ZERO;
		for (CreditLoanDetail creditLoanDetail : listLoanDetail) {
			if(!"正常".equals(creditLoanDetail.getAccountStatus())){
				continue;
			}
			List<String> list = Arrays.asList("个人住房贷款","个人商用房（包括商住两用）贷款","个人住房公积金贷款"); 
			BigDecimal contractAmount = new BigDecimal(creditLoanDetail.getContractAmount());
			if(list.contains(creditLoanDetail.getCreditCardType())){ //除掉购房贷款
				contractAmount = BigDecimal.ZERO;
			}
			noSettleTotal = noSettleTotal.add(contractAmount);
			if("个人汽车贷款".equals(creditLoanDetail.getCreditCardType())){
				noSettleCarTotal = noSettleCarTotal.add(contractAmount);
			}
			
		}
		noPledgeLoanAmount = noSettleTotal.subtract(noSettleCarTotal).setScale(2, BigDecimal.ROUND_HALF_UP);
		
		Connection connection =JdbcUtils.getConnectionJdbc();
		PreparedStatement ps = connection.prepareStatement("update uc_person_info set no_pledge_loan_amount=? where user_id=? ");
		ps.setBigDecimal(1,noPledgeLoanAmount);
		ps.setString(2,userId);
		int updateCount = ps.executeUpdate();
		if(updateCount>0){
			logger.info("无抵押贷款总额因子更新成功！");
			rtFlag = true;
		}
		return rtFlag;
	}
	
	
	
}
